package com.xiahui.day01;

/**
 * 位运算符
 * << 左移：左移表示乘以2的几次方 有可能出现负数的情况
 * >> 右移：右移表示除以2的几次方
 * >>> 无符号右移 不管最高位是0还是1 空缺位统一以0填充 即 最终结果都是一个正数
 * 如果最高位是1 则移动完以后 空缺位补1
 * 如果最高位是0 则移动完以后 空缺位补0
 */
public class TestBitOperator {
    public static void main(String[] args) {
        int x = 1;
        System.out.println(x << 1);
        System.out.println(x << 2);
        System.out.println(x << 3);
        System.out.println(x << 4);
        System.out.println(x << 31);
        System.out.println(x << 32);

        System.out.println("---------------------------------------");

        System.out.println(8>>1);
        System.out.println(8>>>1);
        System.out.println(8>>2);
        System.out.println(8>>3);
        System.out.println(8>>4);
        System.out.println(8>>5);


        int a = 60;       /* 60 = 0011 1100 */
        int b = 13;       /* 13 = 0000 1101 */
        int c = 0;

        c = a & b;        /* 12 = 0000 1100 */
        System.out.println("a & b = " + c);

        c = a | b;        /* 61 = 0011 1101 */
        System.out.println("a | b = " + c);

        c = a ^ b;        /* 49 = 0011 0001 */
        System.out.println("a ^ b = " + c);

        c = ~a;           /*-61 = 1100 0011 */
        System.out.println("~a = " + c);

        c = a << 2;       /* 240 = 1111 0000 */
        System.out.println("a << 2 = " + c);

        c = a >> 2;       /* 15 = 0000 1111 */
        System.out.println("a >> 2  = " + c);

        c = a >>> 2;     /* 15 = 0000 1111 */
        System.out.println("a >>> 2 = " + c);

        int d = -60;
        c = d >> 2; //有符号右移
        System.out.println("d >> 2 = "+ c); //-15
        c = d >>>2; //无符号右移
        System.out.println("d >>> 2 = "+ c); //1073741809
    }
}
